package com.fc.campusapplicationsystem.util;

import com.fc.campusapplicationsystem.model.ReList;
import com.fc.campusapplicationsystem.model.Superuser;
import com.fc.campusapplicationsystem.service.MailService;
import com.fc.campusapplicationsystem.service.ReListService;
import com.fc.campusapplicationsystem.service.SuperuserService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author kyc
 * 2020-06-23
 * 报修单超时通知上级处理
 */
public class MonitorUtil {

    private static ReadConfigUtil re = null;

/*    @Autowired
    private static MailService mailService;

    @Autowired
    private static ReListService reListService;

    @Autowired
    private static SuperuserService superuserService;*/


    /**
     * 创建线程池
     */
    static ExecutorService fixedThreadPool = Executors.newCachedThreadPool();

    /**
     * 通知上级领导
     * @param reList 报修单
     */
    public static void informToOn(ReList reList) {
        SuperuserService superuserService = GetBeanUtil.getApplicationContext().getBean(SuperuserService.class);
        ReListService reListService = GetBeanUtil.getApplicationContext().getBean(ReListService.class);
        MailService mailService = GetBeanUtil.getApplicationContext().getBean(MailService.class);
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    if (reList.getState() == 0) {
                        // 新增报修单(多久时间未处理通知经理)
                        Thread.sleep(30000);
                        ReList r = reListService.getById(reList.getId());
                        if (r.getState() == 0){
                            // todo 发送邮件到经理
                            System.out.println("新单超时处理（经理）");
                            Superuser superuser = superuserService.getByType(1);
                            List<String> recipientTo = new ArrayList<>();
                            recipientTo.add(superuser.getSuperuserEmail());
                            re = new ReadConfigUtil("application.properties");
                            mailService.sendMail(re.getValue("exchange.account"), re.getValue("exchange.password"),
                                    "chery",re.getValue("exchange.emailAddress"), "工单超时未分配",
                                    recipientTo, "有新的工单超时未指派处理!");
                        }
                    } else {
                        // 指派报修单(多久时间未处理通知经理)
                        Thread.sleep(40000);
                        ReList r = reListService.getById(reList.getId());
                        if (r.getState() != Constants.Number.TWO){
                            // todo 发送邮件到经理
                            System.out.println("新单超时解决（经理）");
                            Superuser superuser = superuserService.getByType(1);
                            List<String> recipientTo = new ArrayList<>();
                            re = new ReadConfigUtil("application.properties");
                            mailService.sendMail(re.getValue("exchange.account"), re.getValue("exchange.password"),
                                    "chery", re.getValue("exchange.emailAddress"), "工单超时未解决", recipientTo, "有工单超时未解决!");
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    if (reList.getState() == 0) {
                        // 新增报修单(多久时间未处理通知总监)
                        Thread.sleep(60000);
                        ReList r = reListService.getById(reList.getId());
                        if (r.getState() == 0){
                            //  发送邮件到总监
                            System.out.println("新单超时处理（总监）");
                            Superuser superuser = superuserService.getByType(2);
                            List<String> recipientTo = new ArrayList<>();
                            recipientTo.add(superuser.getSuperuserEmail());
                            re = new ReadConfigUtil("application.properties");
                            mailService.sendMail(re.getValue("exchange.account"), re.getValue("exchange.password"),
                                    "chery", re.getValue("exchange.emailAddress"), "工单超时未分配", recipientTo, "有新的工单超时未指派处理!");
                        }
                    } else {
                        // 指派报修单(多久时间未处理通知总监)
                        Thread.sleep(80000);
                        ReList r = reListService.getById(reList.getId());
                        if (r.getState() != Constants.Number.TWO){
                            // 发送邮件到总监
                            System.out.println("新单超时解决（总监）");
                            Superuser superuser = superuserService.getByType(2);
                            List<String> recipientTo = new ArrayList<>();
                            recipientTo.add(superuser.getSuperuserEmail());
                            re = new ReadConfigUtil("application.properties");
                            mailService.sendMail(re.getValue("exchange.account"), re.getValue("exchange.password"),
                                    "chery", re.getValue("exchange.emailAddress"), "工单超时未解决", recipientTo, "有工单超时未解决!");
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
